﻿using ET;

namespace Core
{
    public enum ItemType
    {
        None,
        Equipment
    }

    public enum ItemSubType
    {
        Armour, //战甲
        Amulet, //护符
        Ring,   //戒指
        Bracer, //护腕
        Shoes,  //鞋子
    }

    [Event]
    public class EquipmentChangeEvent : AEvent<ItemChange>
    {
        protected override async ETTask Run(ItemChange itemChange)
        {
            var owner = itemChange.container.Owner;
            var inventory = owner.GetComponent<InventoryComponent>();
            if (inventory.equipment != itemChange.container)
                return;

            var configComponent = World.GetComponent<ConfigComponent>();
            UpdateConfig(owner, configComponent.GetConfig<ItemConfig>(itemChange.oldItemId), false);
            UpdateConfig(owner, configComponent.GetConfig<ItemConfig>(itemChange.newItemId), true);

            var window = await World.GetComponent<FGUIComponent>().GetWindowAsync<MainWindow>();
            if (window.roleInfoWidget.Owner == owner)
            {
                window.roleInfoWidget.UpdateInfo();
            }
        }

        private void UpdateConfig(Unit owner, ItemConfig config, bool isAdd)
        {
            if (config == null || config.itemType != (int)ItemType.Equipment)
                return;

            if(isAdd)
            {
                owner.GetComponent<SkillComponent>().AddSkill(config.skillId);
                owner.GetComponent<BuffComponent>().AddBuff(config.buffid);
            }
            else
            {
                owner.GetComponent<SkillComponent>().RemoveSkill(config.skillId);
                owner.GetComponent<BuffComponent>().RemoveBuff(config.buffid);
            }
        }
    }
}
